package com.wugh.fast.generator;

import java.util.Objects;

/**
 * 说明:
 *
 * @author wugh
 * @date 2019-11-15 17:17
 */
public class SQLiteUtil {
    /**
     * 处理名字,改成驼峰命名法
     * <pre>
     *     如:
     *     ID       -> id
     *     Id       -> id
     *     UserId   -> userId
     *     PrjGUID  -> prjGuid
     *     IArea    -> iArea
     *     AZLay    -> azLay
     *     AZLayGUID    -> azLayGuid
     * </pre>
     *
     * @param name
     * @return
     */
    public static String handleName(String name) {
        if (name.chars().allMatch(Character::isUpperCase)) {
            //全部都是大写字母
            return name.toLowerCase();
        }
        StringBuilder builder = new StringBuilder();
        Boolean pre = null;
        for (int i = 0; i < name.length(); i++) {
            char c = name.charAt(i);
            boolean cur = Character.isUpperCase(c);
            if (!cur) {
                //当前不是大写
                builder.append(c);
            } else if (pre == null) {
                builder.append(Character.toLowerCase(c));
            } else {
                //当前是大写
                if (!pre) {
                    //前面是小写,当前保留大写
                    builder.append(c);
                } else {
                    //前面大写
                    //后面是否是大写
                    boolean next = (i + 1) == name.length() || Character.isUpperCase(name.charAt(i + 1));
                    if (next) {
                        //如果后面大写,当前需要变成小写
                        builder.append(Character.toLowerCase(c));
                    } else {
                        //后面小谢,则保持当前也是大写
                        builder.append(c);
                    }
                }
            }
            pre = cur;
        }
        return builder.toString();
    }

    public static void main(String[] args) {
        System.out.println("ID--->" + handleName("ID") + ",expect: id---" + Objects.equals(handleName("ID"), "id"));
        System.out.println("Id--->" + handleName("Id") + ",expect: id---" + Objects.equals(handleName("Id"), "id"));
        System.out.println("UserId--->" + handleName("UserId") + ",expect: userId---" + Objects.equals(handleName("UserId"), "userId"));
        System.out.println("PrjGUID--->" + handleName("PrjGUID") + ",expect: prjGuid---" + Objects.equals(handleName("PrjGUID"), "prjGuid"));
        System.out.println("IArea--->" + handleName("IArea") + ",expect: iArea---" + Objects.equals(handleName("IArea"), "iArea"));
        System.out.println("AZLay--->" + handleName("AZLay") + ",expect: azLay---" + Objects.equals(handleName("AZLay"), "azLay"));
        System.out.println("AZLayGUID--->" + handleName("AZLayGUID") + ",expect: azLayGuid---" + Objects.equals(handleName("AZLayGUID"), "azLayGuid"));
    }
}
